﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Genealogy_Builder
{

    public partial class RelationDialog : Form
    {
        bool pass = true;
        public Relation newRelation;

        List<Person> PeopleA;
        List<Person> PeopleB;

        public RelationDialog(ListBox People)
        {
            InitializeComponent();
            PeopleA = new List<Person>();
            PeopleB = new List<Person>();
            foreach (Person p in People.Items)
            {
                PeopleA.Add(p);
                PeopleB.Add(p);
            }
            cmbPersonA.DataSource = PeopleA;
            cmbPersonB.DataSource = PeopleB;
        }

        public void AddPersonA(Person A)
        {
            cmbPersonA.SelectedIndex = cmbPersonA.FindStringExact(A.ToString());
        }
        public void AddPersonB(Person B)
        {
            cmbPersonB.SelectedIndex = cmbPersonB.FindStringExact(B.ToString());
        }
        public bool AddRelation()
        {
            this.ShowDialog();
            return pass;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            pass = false;
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (cmbPersonA.SelectedItem == null || cmbPersonB.SelectedItem == null || cmbRoleA.Text == "" || cmbRoleB.Text == "" || cmbType.Text == "")
            {
                MessageBox.Show("Person A, Person B, Role A, Role B, and Type must have values.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                newRelation = new Relation((Person)cmbPersonA.SelectedItem ,(Person)cmbPersonB.SelectedItem,cmbRoleA.Text,cmbRoleB.Text,cmbType.Text,
                    dateStartDate.Value,
                    (chkEndDate.Checked ? dateStartDate.Value : DateTime.MinValue));
                this.Close();
            }
        }

        private void btnSwap_Click(object sender, EventArgs e)
        {
            Person A = (Person)cmbPersonB.SelectedItem;
            Person B = (Person)cmbPersonA.SelectedItem;
            cmbPersonA.SelectedIndex = cmbPersonA.FindStringExact(A.ToString());
            cmbPersonB.SelectedIndex = cmbPersonB.FindStringExact(B.ToString());
        }

        private void chkEndDate_CheckedChanged(object sender, EventArgs e)
        {
            dateEndDate.Enabled = chkEndDate.Checked;
        }
    }
}
